﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class entry_student : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string Sno = TextBox1.Text.Trim();
        string Sname = TextBox2.Text.Trim();
        string Sclass = TextBox3.Text.Trim();
        string Ssex = RadioButtonList1.Text.Trim();
        string Smajor = TextBox5.Text.Trim();
        string Syear = TextBox6.Text.Trim();

        string conn = "Data Source= . ;Initial Catalog=XSXK; User ID=sa; pwd=123";
        SqlConnection connection = new SqlConnection(conn);



        try
        {
            if (string.IsNullOrEmpty(Sno) || string.IsNullOrEmpty(Sname) || string.IsNullOrEmpty(Sclass) || string.IsNullOrEmpty(Ssex) || string.IsNullOrEmpty(Smajor) || string.IsNullOrEmpty(Syear) )
            {
                Response.Write("<h1>添加失败，学生的任一项信息不能为空！！</h1>");
            }
            else
            {
                
                connection.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = connection;

                string str_sql = "insert into Student(Sno , Sname , Sclass , Ssex , Smajor , Syear) values(  '" + Sno + "' ,  '" + Sname + "'  ,  '" + Sclass + "'  , '" + Ssex + "'  ,  '" + Smajor + "'  , '" + Syear + "' )  ";
                comm.CommandText = str_sql;
                //comm.ExecuteScalar();  //如果为查询语句，则用ExecuteScalar()；否则，用ExecuteNonQuery()
                comm.ExecuteNonQuery();

                Response.Write("<h1>成功添加学生信息！</h1>");
            }
        }

        catch (SqlException ex)
        {
            Response.Write(ex.Message);
        }

        connection.Close();//关闭数据库连接
   
    }
}